package com.muyu.user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.exception.base.BaseException;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.user.domain.SysUser;
import com.muyu.user.domain.SysUserGroupRelation;
import com.muyu.user.domain.SysUserTagRelation;
import com.muyu.user.domain.menu.DelFlagMenu;
import com.muyu.user.domain.req.*;
import com.muyu.user.domain.resp.SysUserPageResp;
import com.muyu.user.mapper.SysUserGroupRelationMapper;
import com.muyu.user.mapper.SysUserManageMapper;
import com.muyu.user.mapper.SysUserTagRelationMapper;
import com.muyu.user.service.SysUserManageService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户管理业务层
 *
 * @author <a href="mailto:1416681852@qq.com">liuguoqiang</a>
 * @version 1.0
 * @description: 用户管理业务层
 * @date 2025/9/14 22:23
 */
@Service
public class SysUserManageServiceimpl extends ServiceImpl<SysUserManageMapper,SysUser> implements SysUserManageService{
    /**
     * 用户管理列表
     */
    @Autowired
    private SysUserManageMapper sysUserManageMapper;
    /**
     * 用户标签中间表
     */
    @Autowired
    private SysUserTagRelationMapper sysUserTagRelationMapper;
    /**
     * 用户分组中间表
     */
    @Autowired
    private SysUserGroupRelationMapper sysUserGroupRelationMapper;
    @Override
    public PageQueryModel<SysUserPageResp> selectUserPageList(SysUserPageReq sysUserPageReq) {
        // 构建分页对象
        Page<SysUserPageResp> page = new Page<>(sysUserPageReq.getPageNum(), sysUserPageReq.getPageSize());
        //用户管理列表
        Page<SysUserPageResp> list = sysUserManageMapper.selectUserPageList(page,sysUserPageReq);
        // 返回分页结果
        return PageQueryModel.of(list);
    }
    /**
     * 给用户批量设置标签
     * @param batchSetTagsReq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void SetUserPlTag(BatchSetTagsReq batchSetTagsReq) {
        //用户
        List<Long> userIds = batchSetTagsReq.getUserIds();
        //标签
        List<Long> tagIds = batchSetTagsReq.getTagIds();
        //判断
        if(StringUtils.isEmpty(userIds) && StringUtils.isEmpty(tagIds)){
            throw new BaseException("用户id和标签id不能为空");
        }
        //若是给用户批量设置 用户已有标签 需要先将原始标签给删除
        sysUserTagRelationMapper.deleteByUserIds(userIds);
        //构造批量插入数据
        List<SysUserTagRelation> userTagRelations = new ArrayList<>();
        //循环遍历
        for (Long userId : userIds) {
            for (Long tagId : tagIds) {
                //构建对象
                SysUserTagRelation sysUserTagRelation = new SysUserTagRelation();
                //用户id
                sysUserTagRelation.setUserId(userId);
                //标签
                sysUserTagRelation.setTagId(tagId);
                //创建时间
                sysUserTagRelation.setCreateTime(new Date());
                //添加到创建对象中
                userTagRelations.add(sysUserTagRelation);
            }
        }
        //批量添加用户标签
        sysUserTagRelationMapper.BatchUserTag(userTagRelations);
    }
    /**
     * 给用户批量设置分组
     * @param batchSetGroupReq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setUserPlGroup(BatchSetGroupReq batchSetGroupReq) {
        //用户
        List<Long> userIds = batchSetGroupReq.getUserIds();
        //分组
        Long groupId = batchSetGroupReq.getGroupId();
        //判断
        if(StringUtils.isEmpty(userIds) && groupId == null){
            throw new BaseException("用户id和标签id不能为空");
        }
        //若是给用户批量设置 用户已有分组 需要先将原始分组给删除
        sysUserGroupRelationMapper.deleteByUserIds(userIds);
        //构造批量插入数据
        List<SysUserGroupRelation> sysUserGroupRelations = new ArrayList<>();
        //循环遍历
        for (Long userId : userIds) {
                SysUserGroupRelation userGroupRelation = new SysUserGroupRelation();
                //用户id
                userGroupRelation.setUserId(userId);
                //分组id
                userGroupRelation.setGroupId(groupId);
                //创建时间
                userGroupRelation.setCreateTime(new Date());
                sysUserGroupRelations.add(userGroupRelation);
        }
        //批量添加分组
        sysUserGroupRelationMapper.BatchUserGroup(sysUserGroupRelations);
    }

    @Override
    public SysUser getUserDetail(Integer userId) {
        return sysUserManageMapper.getUserDetail(userId);
    }
    /**
     * 设置用户等级
     */
    @Override
    public void setUserLevel(SysUserSetLevelReq sysUserSetLevelReq) {
        //使用了valid不能为空
        //修改用户的等级
        sysUserManageMapper.setUserLevel(sysUserSetLevelReq);
    }

    /**
     * 用户编辑
     * @param sysUserUpdReq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setUserUpd(SysUserUpdReq sysUserUpdReq) {
        //非空判断用valid
        sysUserManageMapper.setUserUpd(sysUserUpdReq);
        //创建分组对象
        BatchSetGroupReq batchSetGroupReq = new BatchSetGroupReq();
        //拷贝
        BeanUtils.copyProperties(sysUserUpdReq,batchSetGroupReq);
        //调用修改分组
        this.setUserPlGroup(batchSetGroupReq);
        //创建标签对象
        BatchSetTagsReq batchSetTagsReq = new BatchSetTagsReq();
        //拷贝
        BeanUtils.copyProperties(sysUserUpdReq,batchSetTagsReq);
        //调用修改标签
        this.SetUserPlTag(batchSetTagsReq);
    }
}
